import currency from 'currency.js'

export const deleteObjNullKey = (obj) => {
	for (let key in obj) {
		if (!obj[key]) delete obj[key]
	}
	return obj
}

// 获取商品分类枚举值对应的字义
export const getLabelOfCategory = (dictValue, categoryMap) => {
	if (!categoryMap) return ''
	return categoryMap.find(_ => {
		return _.dictValue === dictValue
	})?.dictLabel || ''
}

/**
 * 将对象中的属性值的数值转化成字符串
 * @param obj {Object} 要转换的对象
 * @param targetPropList {Array} 需要转换的属性数组
 */
export const propNumberConverToString = (targetObj, targetPropList = []) => {
	const resObj = JSON.parse(JSON.stringify(targetObj))
	for (let [key, value] of Object.entries(resObj)) {
		if (targetPropList.includes(key) && (value || value == 0)) {
			resObj[key] = '' + value
		}
	}
	return resObj
}

/**
 * 将对象中的属性值的字符串转化成数值
 * @param obj {Object} 要转换的对象
 * @param targetPropList {Array} 需要转换的属性数组
 */
export const propStringConverToNumber = (targetObj, targetPropList) => {
	const resObj = JSON.parse(JSON.stringify(targetObj))
	for (let [key, value] of Object.entries(resObj)) {
		if (targetPropList.includes(key) && (value || value == 0)) {
			resObj[key] = currency(value).value
		}
	}
	return resObj
}